const express = require('express')
const Result = require('../models/Result')
const {
  orLike,
  orLimit,
  queryTotal,
  querySqlTotal,
  querySql,
  insert,
  deleteData,
  update,
  queryOne
} = require('../db/index')

const router = express.Router()
const path = require('path');
const fs = require('fs');
/**
 * 批量增加歌词
 */
router.post('/batchWrite', async(req, res) => {
  function readDirSync(path, item){
    var pa = fs.readdirSync(path);
    pa.forEach(async (ele) => {
      let temp = path+'/'+ele
      temp = ele.substr(0, ele.length-4)
      if(temp === item.name) {
        let text = fs.readFileSync(path+'/'+ele).toString()
        // 新增
        await insert(`insert into lyric values(null, '${item.id}', '${text}')`, 'lyricId')
      }
    })
  }

  let result = await querySql(`SELECT * FROM song WHERE album = ${req.body.album}`)

  result.forEach(item => {
    readDirSync(req.body.catalogue, item)
    console.log(item.name)
  })

  new Result(true, '写入成功').success(res)
})


module.exports = router